package com.css.fxfzysx.modules.ysxManagement.repository.impl;

import com.css.fxfzysx.modules.vabStructure.constans.CorrectionCiEnum;
import com.css.fxfzysx.modules.ysxManagement.entity.CStructureCiEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.CnRatioEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.DynamicFieldsEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.ZqtEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.ZqtCiRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Repository
public class ZqtCiRepositoryImpl implements ZqtCiRepository {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void saveZqt(ZqtEntity it) {
        jdbcTemplate.update("insert into czt_brick_resistance_main(cbrm_id,condition,create_user,create_time,del_flag,ci_value,project_id,default_data) " +
                "values(?,?,?,?,?,?,?,?)", it.getCbrmId(), it.getCondition(), it.getCreateUser(), it.getCreateTime(), it.getDelFlag(),it.getCiValue(),it.getProjectId(),it.getDefaultData());
    }

    @Override
    public void saveDynamicFields(DynamicFieldsEntity it) {
        jdbcTemplate.update("insert into czt_brick_resistance_sub (cbrs_id,info,create_user,create_time,del_flag,cbrs_key,cbrs_column,default_data) " +
                "values (?,?,?,?,?,?,?,?)", it.getCbrsId(), it.getInfo(), it.getCreateUser(), it.getCreateTime(), it.getDelFlag(),it.getCbrsKey(),it.getCbrsColumn(),it.getDefaultData());
    }

    @Override
    public List<ZqtEntity> getZqt() {
        String sql = "select * from czt_brick_resistance_main where del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZqtEntity.class));
    }

    @Override
    public List<DynamicFieldsEntity> getDynamicFields(String titleName) {
        String sql = "select * from czt_brick_resistance_sub where cbrs_key =? and del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(DynamicFieldsEntity.class),titleName);
    }

    @Override
    public void updateZqtCi(ZqtEntity it) {
        String sql = "update czt_brick_resistance_main set condition= :condition,update_user= :updateUser,update_time= :updateTime,ci_value= :ciValue,project_id= :projectId" +
                " where cbrm_id= '"+it.getCbrmId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void updateDynamicFields(DynamicFieldsEntity it) {
        String sql = "update czt_brick_resistance_sub set info= :info,update_user= :updateUser,update_time= :updateTime,cbrs_key= :cbrsKey,cbrs_column= :cbrsColumn" +
                " where cbrs_id='"+it.getCbrsId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void deleteZqtCiById(String zqtId) {
        String sql = "update czt_brick_resistance_main set del_flag = '1' where cbrm_id=?";
        jdbcTemplate.update(sql,zqtId);
    }

    @Override
    public List<ZqtEntity> getZqtById(String zqtIds) {
        ArrayList<ZqtEntity> data = new ArrayList<>();
        for (String zqtId : zqtIds.split(",")) {
            String sql = "select * from czt_brick_resistance_main where del_flag='0' and cbrm_id='" + zqtId + "'";
            try {
                data.add(jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(ZqtEntity.class)));
            } catch (DataAccessException e) {
                return null;
            }
        }
        return data;
    }

    @Override
    public void deleteArrangeById(String arrangeId) {
        String sql = "update czt_brick_resistance_sub set del_flag = '1' where cbrs_id=?";
        jdbcTemplate.update(sql,arrangeId);
    }

    @Override
    public void deleteZqtCiByProjectId(String arrangeIds) {
        String sql = "update czt_brick_resistance_main set del_flag = '1' where project_id=?";
        jdbcTemplate.update(sql,arrangeIds);
    }

    @Override
    public ZqtEntity getZqtByCondition(String ci, String condition) {
        String sql = "select cbrm_id,ci_value from czt_brick_resistance_main cbm left join czt_brick_resistance_sub cbr on cbm.project_id = cbr.cbrs_id where condition = '"+condition+"' and info = '"+ci+"' and cbm.del_flag='0'";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(ZqtEntity.class));
    }

    @Override
    public CStructureCiEntity getCsByCondition(String ci, String condition) {
        String sql = "select ccsr_id,condition_value from czt_concrete_struct_ratio_main cbm left join czt_brick_resistance_sub cbr on cbm.project_id = cbr.cbrs_id where condition = '"+condition+"' and info = '"+ci+"' and cbm.del_flag='0'";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(CStructureCiEntity.class));
    }

    @Override
    public ZqtEntity getZqtByConditionAndValue(String ciType1, String byCode) {
        try {
            String sql = "SELECT cm.cbrm_id,ci_value FROM czt_brick_resistance_main cm LEFT JOIN czt_brick_resistance_sub cs ON cs.cbrs_id = cm.project_id WHERE cs.cbrs_key = '砖砌体结构抗力修正系数Ci值' and cm.condition = '" + ciType1 + "' and cs.info = '" + byCode + "' AND cs.del_flag = '0' AND cm.del_flag = '0'";
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(ZqtEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }
}
